﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CI.Caja.BL;
using CI.Caja.BE;

namespace CI.Caja.MVC.Controllers
{
    public class PagoController : Controller
    {

        public IBL_Orden OrdenService { get; set; }
        public IBL_Paciente PacienteService { get; set; }
        public IBL_OrdenDetalle OrdenDetalleService { get; set; }
        public IBL_Comprobante ComprobanteService { get; set; }
        //
        // GET: /Pago/

        public ActionResult Index()
        {
            TempData["FormaPagos"] = ListarFormaPago();
            return View();
        }

        //
        // GET: /Pago/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Pago/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Pago/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Pago/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Pago/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Pago/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Pago/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        [HttpPost]
        public ActionResult RegistrarPago (FormCollection collection){
            

            BE_Comprobante prmComprobaneBE = new BE_Comprobante();
            int iResp = 0;
            string txtTotal;

            //Datos del comprobante de pago
            prmComprobaneBE.Serie = "275";
            prmComprobaneBE.Numero = "1";
            prmComprobaneBE.IdPaciente = (string)TempData["Paciente"];
            prmComprobaneBE.Nombre = (string)TempData["NombrePaciente"];
            prmComprobaneBE.NroOrden = (string)TempData["NroOrden"];// collection["txtNroOrden"];
            prmComprobaneBE.IdMedioPago = (string) collection["FormaPago"];
            prmComprobaneBE.IdModalidadPago = "1";
            txtTotal = (string)collection["txtTotal"];
            
            prmComprobaneBE.ImporteIgv = Convert.ToDecimal(txtTotal) / Convert.ToDecimal(1.19);
            prmComprobaneBE.ImporteTotal = Convert.ToDecimal(txtTotal);

            iResp = ComprobanteService.RegistrarComprobante(prmComprobaneBE);
            return View("Index");
        }

        //
        // POST: /Cliente/Search
        [HttpPost]
        public ActionResult Search(FormCollection collection)
        {
            string nroOrden = collection["txtOrden"];
            BE_Orden orden = new BE_Orden();
            BE_Paciente paciente = new BE_Paciente();
            orden.NroOrden = nroOrden;
            ViewData["NroOrden"] = nroOrden;
            TempData["NroOrden"] = nroOrden;
            orden = OrdenService.ObtenerOrden(orden);
            if (orden.IdPaciente != null)
            {
                paciente.IdPaciente = orden.IdPaciente;

                paciente = PacienteService.ObtenerPaciente(paciente);
                if (paciente != null)
                {
                    ViewData["Paciente"] = paciente.Codigo;
                    TempData["Paciente"] = paciente.Codigo;
                    ViewData["NombrePaciente"] = paciente.Nombre;
                    TempData["NombrePaciente"] = paciente.Nombre + " "  + paciente.Apellido;
                    ViewData["Seguro"] = paciente.Seguro;
                    ViewData["Apellidos"] = paciente.Apellido;
                }

                List<BE_OrdenDetalle> detalle = OrdenDetalleService.ObtenerOrdenDetalle(orden);
                TempData["Detalle"] = detalle;
                TempData["FormaPagos"] = ListarFormaPago();
                ViewData["Total"] = orden.ImporteTotal;

                return View("Index");
                
            }
            else
            {
         
                TempData["FormaPagos"] = ListarFormaPago();
                return View("Index");

                //}


            }


        }

        private ICollection<SelectListItem> ListarFormaPago()
        {
            ICollection<SelectListItem> FormaPagos = new List<SelectListItem>();
            FormaPagos.Add(new SelectListItem() { Value = "1", Text = "Efectivo" });
            FormaPagos.Add(new SelectListItem() { Value = "2", Text = "Tarjeta de Crédito" });
            FormaPagos.Add(new SelectListItem() { Value = "3", Text = "Tarjeta de Débito" });

            return FormaPagos;

        }
    }
}
